﻿using System.IO;
using System.Text;
using System.Text.Json;

namespace XYAC.Framework.WohuaMesExtend.IHI.Extensions
{
    public static class JsonSerializeHelper
    {
        public static string SerializeObjectToJson(object source)
        {
            try
            {
                var serializeStr = JsonSerializer.Serialize(source);
                return serializeStr;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static string SerializeObjectToJson(object source, bool compress)
        {
            try
            {
                var options = new JsonSerializerOptions
                {
                    WriteIndented = !compress // 当 compress 为 true 时，不缩进
                };
                var serializeStr = JsonSerializer.Serialize(source, options);
                return serializeStr;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 将实体序列化为 BSON 格式的字符串
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string SerializeObjectToBson(object source)
        {
            // System.Text.Json 不支持 BSON 序列化，此方法无法直接实现
            throw new NotImplementedException("System.Text.Json 不支持 BSON 序列化。");
        }

        public static TTarget DeserializeObjectFromJson<TTarget>(string json)
        {
            try
            {
                if (json is null)
                {
                    return default(TTarget);
                }
                return JsonSerializer.Deserialize<TTarget>(json);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static TTarget DeserializeObjectFromFile<TTarget>(string filePath)
        {
            try
            {
                using (FileStream fs = File.OpenRead(filePath))
                {
                    return JsonSerializer.Deserialize<TTarget>(fs);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static TTarget DeserializeObjectFromStream<TTarget>(Stream stream)
        {
            try
            {
                stream.Position = 0;
                return JsonSerializer.Deserialize<TTarget>(stream);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static T DeserializeObjectFromBson<T>(string bsonBase64data)
        {
            // System.Text.Json 不支持 BSON 反序列化，此方法无法直接实现
            throw new NotImplementedException("System.Text.Json 不支持 BSON 反序列化。");
        }

        public static string SerializeObjectToFile(object source, string fileName)
        {
            try
            {
                string result = string.Empty;
                var serializeStr = JsonSerializer.Serialize(source);
                using (var fileStream = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    var writeContent = Encoding.UTF8.GetBytes(serializeStr);
                    fileStream.Write(writeContent, 0, writeContent.Length);
                    result = serializeStr;
                }
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
